package com.mobis.user.controller;


import com.mobis.user.pojo.User;
import com.mobis.user.service.UserService;
import com.sun.org.apache.regexp.internal.REUtil;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/register/{code}",method = RequestMethod.POST)
    public Result addUser(@RequestBody User user, @PathVariable String code){
        System.out.println("接受用户注册信息");
        userService.saveUser(user);

        return  new Result(true, StatusCode.OK,"添加用户成功");
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result userLogin(@RequestBody User user){

        if (null==user.getMobile()||"".equals(user.getMobile().trim())){
            return new Result(false,StatusCode.ERROR,"手机号不能为空");
        }

        User user1 = userService.loginUser(user);
        if (null!=user1){

            //生成token
            String token=jwtUtils.createJWT(user.getId(),user.getMobile(),"admin");
            Map<String,String> map=new HashMap<>();
            map.put("token",token);
            map.put("mobile",user.getMobile());
            map.put("role","admin");
            return new Result(true,StatusCode.OK,"登陆成功",map);
        }



        return  new Result(false,StatusCode.ERROR,"登陆失败");
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody User user){

        if (null==user.getMobile()||"".equals(user.getMobile().trim())){
            return new Result(false,StatusCode.ERROR,"手机号不能为空");
        }

        userService.saveUser(user);
        return new Result(true,StatusCode.OK,"添加用户成功");
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id){
        String authHeader = request.getHeader("Authorization");//获取头信息

            userService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @RequestMapping(value = "/incfans/{userid}/{x}",method = RequestMethod.POST)
    public Result addFansCount(@PathVariable String userid,@PathVariable int x){
        userService.incFansCount(x,userid);
        return new Result(true,StatusCode.OK,"添加粉丝成功");
    }

    @RequestMapping(value = "/incfollow/{userid}/{x}",method = RequestMethod.POST)
    public Result addFollowCount(@PathVariable String userid,@PathVariable int x){
        userService.incFollowCount(x,userid);
        return new Result(true,StatusCode.OK,"更新成功");
    }


}
